
package code.codeworld.search.web_search.controller;


import code.codeworld.search.web_search.model.vo.WebSearchResultVo;
import code.codeworld.search.web_search.model.vo.WebSearchVo;
import code.codeworld.search.web_search.service.WebSearchService;
import code.codeworld.common.kits.Request;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;



/**
 * WebSearch控制器
 *
 * @author freecode
 * @date 2023/03/23 00:20
 */
@CrossOrigin
@RequestMapping(Request.CODEWORLD_WEB)
@RestController
@Validated
@Slf4j
public class WebSearchCtl {
    @Resource
    private WebSearchService webSearchService;


    /**
     * 搜索WebSearchVo
     *
     * @author freecode
     * @date 2023/03/23 00:20
     */
    @CrossOrigin
    @PostMapping("/webSearch/search")
    public Page<WebSearchResultVo> search(@RequestBody @Valid WebSearchVo webSearchVo, HttpServletRequest httpRequest) throws Exception {
        String keyword = webSearchVo.getKeyword();
        if (StringUtils.isEmpty(keyword)) {
            throw new Exception("搜索词为空");
        }
        log.info("webSearch_httpRequest" + httpRequest);
        String ip = Request.getIpAddress(httpRequest);
        webSearchVo.setCreateIp(ip);

        return webSearchService.search(webSearchVo);
    }


}
